﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class School_Manage_InputScore_xml_download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["filepath"] != null)
            {
                string path = Session["filepath"].ToString(); ;
                string temp_path = HttpContext.Current.Server.MapPath("xls_files");//
                string save_path = temp_path + "\\" + path;
                DownLoad(save_path);
            }
        }
    }

    private void DownLoad(string path)
    {
        if (path != null && File.Exists(path))
        {
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            //清除缓冲区流中的所有内容输出

            Response.Clear();

            //将下载保存对话框指定默认的文件名添加到HTTP头中
            //Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

            Response.AddHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));//避免中文出现乱码现象   

            //在header中指定文件的大小，使浏览器能显示下载过程
            Response.AddHeader("Content-Length", file.Length.ToString());

            //设置输出流的 HTTP MIME 类型
            Response.ContentType = "application/octet-stream";

            // 发送文件流到客户端
            Response.WriteFile(file.FullName);
            // 停止该页的执行

            Response.End();

        }
    }
}
